<?php
require_once "managerSession.php";
$Item="管理员管理";
$search="";
$arr=[];
if (isset($_REQUEST["search"])&&!empty($search=trim($_REQUEST["search"]))){
    $arr=$core->GetManagerBySearch($search);
}else{
    $arr=$core->GetAllManage();
}

if (!is_array($arr[0])){
    $arr=[];
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>娱乐会所吧台管理系统</title>
    <?php include_once "link.php"; ?>
</head>
<body class="container">
<?php require_once "Nav.php"; ?>
<div class="main">
    <?php include_once "menu.php"; ?>
    <div class="pr inlb bd col-xs-10">
        <table class="table table-striped">
            <caption>
                管理员管理
                <form id="search" class="pull-right" action="">
                    <input type="text" name="search" value="<?php echo $search; ?>">
                    <button>搜索</button>
                </form>
            </caption>
            <tr>
                <th>姓名</th>
                <th>微信</th>
                <th>权限</th>
                <th>操作</th>
            </tr>
            <?php
            for($i=0;$i<count($arr);++$i){
            ?>
            <tr>
                <td><?php echo $arr[$i]["name"]; ?></td>
                <td><?php echo $arr[$i]["weixin_id"]; ?></td>
                <td><?php echo $arr[$i]["role"]=="普通"?"前台收银":$arr[$i]["role"]; ?></td>
                <td><button class="btn btn-primary" onclick="edit(<?php echo $arr[$i]["id"]; ?>)" data-toggle="modal" data-target="#mgrDg" <?php echo $manager["role"]!="超级管理员"?"disabled=\"disabled\"":""; ?> >编辑</button>
                    <?php if($manager["role"]=="超级管理员"){ ?>
                <button class="btn btn-primary" onclick="delManager(<?php echo $arr[$i]["id"]; ?>)"   >删除</button></td>
                <?php } ?>
            </tr>
            <?php } ?>
        </table>

    </div>
</div>
<?php
dialogHeader("mgrDg","?search=$search", "管理员信息修改","sm"); ?>
<div class="modal-body">
    <input  type="text" name="Mid" style="display: none">
    <div class="form-group">
        <label class="control-label col-xs-4">名称</label>
        <div class="col-xs-8">
            <input  type="text"  class="form-control " name="name" required>
        </div>
    </div>



    <div class="form-group">
        <label class="control-label col-xs-4">权限</label>
        <div class="col-xs-8">
            <select class="form-control" name="role">
                <option value="普通">前台收银</option>
                <option value="管理员">管理员</option>
                <option value="超级管理员">超级管理员</option>
            </select>
        </div>
    </div>
</div>
<?php
dialogFooter(1);
?>
<div id="msg" class="modal fade">
    <div class="modal-dialog modal-sm" >
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-title">
                    提示
                </div>
            </div>
            <div id="msgContent" class="modal-body">
                <?php echo $message; ?>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="button" onclick="reload()" data-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
</div>

<div id="del" class="modal fade">
    <div class="modal-dialog modal-sm" >
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-title">
                    提示
                </div>
            </div>
            <div id="msgContentDel" class="modal-body">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="button" data-dismiss="modal" onclick="del()">确定</button>
                <button class="btn btn-primary" type="button" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<script>
    var oid=<?php echo $manager["id"]; ?>;
    var did;
    function edit(id) {
        $.post("getMessage.php",{managerEdit:id},function (o) {
            Use()
            var js=JSON.parse(o)
            $("input[name='Mid']").val(js.id)
            $("input[name='name']").val(js.name)
            $("option[value="+js.role+"]").attr("selected","selected")
            if(js.oRole!="超级管理员"){
                NonUse()
            }
        })
    }
    function NonUse() {
        $("input[name='name']").attr("disabled","disabled")
        $("select").attr("disabled","disabled")
        $(".ds").hide()
    }
    function Use() {
        $("input[name='name']").attr("disabled",false)
        $("select").attr("disabled",false)
        $(".ds").show()
    }

    function delManager(id) {
        $.post("getMessage.php",{del:id,manager:oid},function (o) {
            if (o!=null&&o!=""){
                var js=JSON.parse(o);
                console.log(js)
                $("#msgContentDel").text("确定删除"+js.name)
                $("#del").modal("show")
                did=id
            }else {
                $("#msgContent").text("你不能这么做")
                $("#msg").modal("show")

            }
        })
    }

    function del() {
        $.post("getMessage.php",{cz:"delManager",del:did,manager:oid},function (o) {
            if (o!=null&&o!=""){
                $("#msgContent").text("删除失败")
                $("#msg").modal("show")

            }else {
                $("#msgContent").text("删除成功")
                $("#msg").modal("show")

            }
        })
    }

    function reload(){
        location.reload(true);
    }

    $(function () {
        if(<?php echo "'".$message."'"; ?>){
            console.log("open")
            $("#msg").modal("show")
        }

        $(".ds").click(function(){
            var Mid=$("input[name=Mid]").val()
            var name=$("input[name='name']").val()
            var role=$("select[name='role']").val()
            console.log(Mid+name+role)
            $.post("getMessage.php",{Mid:Mid,name:name,role:role},function(o){
                console.log(o)
                $("#msgContent").text(o)
                $("#mgrDg").modal("hide")
                $("#msg").modal("show")
            })
            return false
        })
    })

</script>
</body>
</html>
